﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Administrator
{
    public partial class ucMeter : UserControl
    {
        private Boolean LoadSetting = false;
        public String Caption = "";
        public Double Precision = 0;
        public Byte BeforeComma = 0;
        public Byte AfterComma;
        public Int32 MeterConst = 0;
        public Byte Phase = 0;
        public Double SelfDemand = 0;
        
        public ucMeter()
        {
            InitializeComponent();
        }

        //Заполнение полей структуры и формы
        public void FillSetting(cMain.METER_SETTING Meter)
        {
            Caption = Meter.Caption;
            Precision = Meter.Precision;            
            BeforeComma = Meter.BeforeComma;
            AfterComma = Meter.AfterComma;
            MeterConst = Meter.MeterConst;
            Phase = Meter.Phase;
            SelfDemand = Meter.SelfDemand;

            if (BeforeComma < 1) BeforeComma = 1;
            if (AfterComma < 1) AfterComma = 1;
            if (Phase < 1) Phase = 1;

            LoadSetting = true;
                        
            tbCaption.Text = Caption;
            nudPricision.Value = (Decimal)Precision;
            nudBeforeComma.Value = BeforeComma;
            nudAfterComma.Value = AfterComma;
            nudConst.Value = MeterConst;
            nudPhase.Value = Phase;
            nudSelfDemand.Value = (Decimal)SelfDemand;

            LoadSetting = false;
        }

        private void tbCaption_TextChanged(object sender, EventArgs e)
        {
            if (!LoadSetting) Caption = ((TextBox)sender).Text;
        }
        private void nudPricision_ValueChanged(object sender, EventArgs e)
        {
            if (!LoadSetting)
            {
                if (sender == nudPricision) Precision = (Double)nudPricision.Value;
                if (sender == nudBeforeComma) BeforeComma = (Byte)nudBeforeComma.Value;
                if (sender == nudAfterComma) AfterComma = (Byte)nudAfterComma.Value;
                if (sender == nudConst) MeterConst = (Int32)nudConst.Value;
                if (sender == nudPhase) Phase = (Byte)nudPhase.Value;
                if (sender == nudSelfDemand) SelfDemand = (Double)nudSelfDemand.Value;
            }
        }
    }
}
